import java.util.Random;

public class Demo1 {
    static int s = 0;
    static int d = 0;
    public static void main(String[] args) throws InterruptedException {
        long time1 = System.currentTimeMillis();
        int[] arr = new int[10_000_000];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(99) + 1;
        }

        Thread t1 = new Thread(() -> {
            // 计算偶数下标的和
            for (int i = 0; i < arr.length; i+=2) {
                s += arr[i];
            }
        });
        Thread t2 = new Thread(() -> {
            // 计算奇数下标的和
            for (int i = 1; i < arr.length; i+=2) {
                d += arr[i];
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("偶数下标的总和->"+s);
        System.out.println("奇数下标的总和->"+d);
        System.out.println("数组元素的总和->"+(s+d));

        long time2 = System.currentTimeMillis();
        System.out.println("花费时间->"+(time2 - time1));
    }
}
